package com.jy.adscm.zltx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.zltx.bean.QsApplyUpdateRecord;
import com.jy.adscm.zltx.bean.QsFile;
import com.jy.adscm.zltx.bean.QsFileContent;
import com.jy.adscm.zltx.bean.QsUpdateHistoryRecord;
import com.jy.adscm.zltx.mapper.mysql.QsApplyUpdateRecordMapper;
import com.jy.adscm.zltx.mapper.mysql.QsFileContentMapper;
import com.jy.adscm.zltx.mapper.mysql.QsFileMapper;
import com.jy.adscm.zltx.mapper.mysql.QsUpdateHistoryRecordMapper;
import com.jy.adscm.zltx.service.QsFileContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 质量体系文件内容 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-10-15
 */
@Service
public class QsFilecontentServiceImpl extends ServiceImpl<QsFileContentMapper, QsFileContent> implements QsFileContentService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    QsFileContentMapper  qsFileContentMapper;
    @Autowired
    QsApplyUpdateRecordMapper qsApplyUpdateRecordMapper;
    @Autowired
    QsFileMapper qsFileMapper;
    @Autowired
    QsUpdateHistoryRecordMapper qsUpdateHistoryRecordMapper;

    @Override
    public List<QsFileContent> selectByFileId(Integer id) {
        return  qsFileContentMapper.selectByFileId(id);
    }


    @Override
    public QsFileContent selectQsFileContentByFileId(Integer fileId, Integer contentType) {
        QsFileContent qsFileContent=new QsFileContent();
          Wrapper wrapper =new EntityWrapper();
        wrapper.eq("fileId",fileId);
        wrapper.eq("contentType",contentType);
        wrapper.eq("isUse","1");
        List<QsFileContent> qsFileContents = qsFileContentMapper.selectList(wrapper);
        if (qsFileContents.size()>0){
            qsFileContent=qsFileContents.get(0);
        }

        return qsFileContent;
    }
   @Transactional
    @Override
    public boolean updateQsFileContent(QsFileContent qsFilecontent) {
        boolean b =true;
        if (qsFilecontent.getIsLocked()==-1){
            b= super.updateById(qsFilecontent);
        }
        if (qsFilecontent.getIsLocked()==0){

            QsFileContent oldqsFilecontent= super.selectById(qsFilecontent.getFileContentId());
            oldqsFilecontent.setIsUse(0);
            super.updateById(oldqsFilecontent);
            b= super.insert(qsFilecontent);
            //新增一条历史修改记录
            //先查询到当前审核通过，没有完成的更新申请
            Wrapper<QsApplyUpdateRecord> wrapper =new EntityWrapper<>();
            wrapper.eq("fileId",qsFilecontent.getFileId());
            wrapper.eq("approvalResult","1");
            wrapper.eq("isFinishUpdate","0");
            List<QsApplyUpdateRecord> qsApplyUpdateRecords = qsApplyUpdateRecordMapper.selectList(wrapper);
            if (qsApplyUpdateRecords.size()>0){
                QsApplyUpdateRecord qsApplyUpdateRecord = qsApplyUpdateRecords.get(0);

                QsUpdateHistoryRecord qsUpdateHistoryRecord =new QsUpdateHistoryRecord();
                qsUpdateHistoryRecord.setFileId(qsFilecontent.getFileId());
                qsUpdateHistoryRecord.setUpdateId(qsApplyUpdateRecord.getId());
                qsUpdateHistoryRecord.setBeforeContent(oldqsFilecontent.getFileContent());
                qsUpdateHistoryRecord.setAfterContent(qsFilecontent.getFileContent());
                qsUpdateHistoryRecord.setContentType(qsFilecontent.getContentType());
                qsUpdateHistoryRecord.setFileUpdateWriter(qsFilecontent.getFileContentWriter());
                qsUpdateHistoryRecord.setUpdateTime(new Date());

                qsUpdateHistoryRecordMapper.insert(qsUpdateHistoryRecord);
            }else {
                new AdscmSystemException("没有找到对应的申请记录");
            }

        }

        return b;
    }
}
